package com.zg.indiv.web;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.R;
import com.zg.common.core.User;
import com.zg.common.core.dao.indiv.entity.IndivSpace;
import com.zg.common.core.dao.indiv.entity.IndivSpaceUser;
import com.zg.common.web.Req;
import com.zg.indiv.domain.req.space.*;
import com.zg.indiv.domain.vo.IndivSpaceCountFileVo;
import com.zg.indiv.domain.vo.IndivSpaceCountRecordVo;
import com.zg.indiv.domain.vo.IndivSpaceUserVo;
import com.zg.indiv.domain.vo.IndivSpaceVo;
import com.zg.indiv.service.IndivSpaceService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.validation.Valid;

/**
* @author lixin
*/
@Api(tags = {"空间管理"})
@RestController
@RequiredArgsConstructor
public class IndivSpaceController {
	private final IndivSpaceService indivSpaceService;

	@ApiOperation("新增空间")
	@ApiResponses({
			@ApiResponse(code = 809001001, message = "请配置空间初始化容量大小"),
			@ApiResponse(code = 809001002, message = "请配置正确的空间初始化容量大小")
	})
	@PostMapping("/space/add")
	public R<Void> add(@RequestBody @Valid IndivSpaceAddReq param) {
		indivSpaceService.add(param);
		return R.success();
	}

	@ApiOperation("修改空间")
	@ApiResponses({
			@ApiResponse(code = 809001003, message = "请选择正确的空间"),
			@ApiResponse(code = 809001004, message = "空间名称已被使用")
	})
	@PostMapping("/space/edit")
	public R<Void> edit(@RequestBody @Valid IndivSpaceEditReq param) {
		indivSpaceService.edit(param);
		return R.success();
	}

	@ApiOperation("增加授权用户")
	@ApiResponse(code = 809001003, message = "请选择正确的空间")
	@PostMapping("/space/addUser")
	public R<Void> addUser(@RequestBody @Valid IndivSpaceAddUserReq param) {
		indivSpaceService.addUser(param);
		return R.success();
	}

	@ApiOperation("查看授权用户")
	@ApiResponse(code = 809001003, message = "请选择正确的空间")
	@PostMapping("/space/listUser")
	public R<List<IndivSpaceUserVo>> listUser(@RequestBody @Valid IndivSpaceListUserReq param) {
		List<IndivSpaceUser> list = indivSpaceService.listUser(param);
		return R.success(BeanUtil.copyToList(list, IndivSpaceUserVo.class));
	}

	@ApiOperation("删除授权用户")
	@ApiImplicitParam(name = "idList", value = "空间用户关联id数组", required = true)
	@PostMapping("/space/removeUser")
	public R<Void> removeUser(@RequestBody List<Long> idList) {
		indivSpaceService.removeUser(idList);
		return R.success();
	}

	@ApiOperation("分页查询空间")
	@PostMapping("/space/page")
	public R<IPage<IndivSpaceVo>> page(@RequestBody @Valid IndivSpacePageReq param) {
		IPage<IndivSpace> page = indivSpaceService.page(param);
		IPage<IndivSpaceVo> result = new Page<>();
		BeanUtil.copyProperties(page, result);
		return R.success(result);
	}

	@ApiOperation("空间扩缩容")
	@ApiResponses({
			@ApiResponse(code = 809001003, message = "请选择正确的空间"),
			@ApiResponse(code = 809001005, message = "请输入正确的容量变化值")
	})
	@PostMapping("/space/editCapacity")
	public R<Void> editCapacity(@RequestBody IndivSpaceEditCapacityReq param) {
		indivSpaceService.editCapacity(param);
		return R.success();
	}

	@ApiOperation("查询授权给我的空间")
	@PostMapping("/space/listMy")
	public R<List<IndivSpaceVo>> listMy() {
		User user = Req.getCurrUser();
		if (user == null) {
			return R.error("请登录后再查询");
		}
		IndivSpaceFindByUserReq param = new IndivSpaceFindByUserReq();
		param.setUserId(user.getId());
		List<IndivSpace> list = indivSpaceService.listByUser(param);
		return R.success(BeanUtil.copyToList(list, IndivSpaceVo.class));
	}

	@ApiOperation("查询授权给某个用户的空间")
	@PostMapping("/space/listByUser")
	public R<List<IndivSpaceVo>> listByUser(@RequestBody @Valid IndivSpaceFindByUserReq param) {
		List<IndivSpace> list = indivSpaceService.listByUser(param);
		return R.success(BeanUtil.copyToList(list, IndivSpaceVo.class));
	}

	@ApiOperation("查询空间文件数及占用存储大小")
	@PostMapping("/space/getCountFile")
	public R<IndivSpaceCountFileVo> getCountFile(@RequestBody @Valid IndivSpaceCountFileReq param) {
		IndivSpaceCountFileVo result = indivSpaceService.getCountFile(param);
		return R.success(result);
	}

	@ApiOperation("查询空间数据量及占用存储大小")
	@PostMapping("/space/getCountRecord")
	public R<IndivSpaceCountRecordVo> getCountRecord(@RequestBody @Valid IndivSpaceCountRecordReq param) {
		IndivSpaceCountRecordVo result = indivSpaceService.getCountRecord(param);
		return R.success(result);
	}

	@ApiOperation("删除空间")
	@ApiResponse(code = 809001006, message = "存在目录无法删除")
	@PostMapping("/space/remove")
	public R<Void> remove(@RequestBody List<Long> idList) {
		indivSpaceService.remove(idList);
		return R.success();
	}
}
